import Vue from 'vue'

/**
 * 判断是否两位数，否则补在前面补0
 * @param {String} n 
 */
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//日期格式过滤
Vue.filter('dateFormat',function(datestr,pattern = ""){
    let dateStr = datestr || new Date();
    var date = new Date(dateStr)
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    if (pattern.toLowerCase() === 'yyyy-mm-dd') { 
        return [year, month, day].map(formatNumber).join('-')
    } else if (pattern.toLowerCase() === 'hh:mm:ss'){
        return [hour, minute, second].map(formatNumber).join(':')
    } else{
        return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
    } 
})

// 数据监控，数值类型，名称转换
Vue.filter('dateTypeFormat',function(dateType){
    if (dateType == 'bit') {
        return '位'
    } else if (dateType == 'int16'){
        return '16位有符号'
    } else if (dateType == 'uint16'){
        return '16位无符号'
    } else if (dateType == 'int32'){
        return '32位有符号'
    } else if (dateType == 'uint32'){
        return '32位无符号'
    } else if (dateType == 'float'){
        return '浮点数'
    }
})
